﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;

namespace web2rss
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Giris("http://www.cawis.sakarya.edu.tr/", "frm_kullanici=g070102039&frm_sifre=22726ka");
        }
        
        private void Giris(string adres, string data)
        {
            Response.Clear();
            String strresponse = "";
            String postData = data;
            byte[] byteArray = Encoding.GetEncoding("ISO-8859-9").GetBytes(postData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(adres);
            request.KeepAlive = true;
            request.CookieContainer = new CookieContainer();
            request.Method = "POST";
            request.Accept = "text/html";
            request.Referer = "http://www.gate.sakarya.edu.tr/";
            request.UserAgent = " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.AllowAutoRedirect = true;
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            request.ContentType = "text/html; charset=ISO-8859-9";
            request.ContentLength = byteArray.Length;


            Stream newStream = request.GetRequestStream();
            // Send the data.
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();

            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            foreach (Cookie cook in response.Cookies)
            {
                request.CookieContainer.Add(cook);
            }

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format.
            StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding("iso-8859-9"));


            strresponse = readStream.ReadToEnd();
            
            Response.Write(strresponse);
            readStream.Close();
            response.Close();

        }
    }
}
